package com.library.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.Result;
import com.library.entity.User;
import com.library.mapper.UserMapper;
import com.library.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2022-05-12
 */
@SuppressWarnings("rawtypes")
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 功能描述: 用户登录
     * @Param: [account, password]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/14 16:28
     */
    public Result userLogin(String account,String password){
        System.out.println("account--->"+account+",password--->"+password);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //根据账号密码查找用户
        wrapper.eq("account",account).eq("password",password);
        User user = getOne(wrapper);
        //如果查找结果不为空，则查找成功
        if (user != null){
            //返回token和用户信息
            return new Result<>().GetToken(user,tokenUtil.getToken(user.getId().toString(),"user"));
        }
        else {
            return new Result<>().OK("用户名或密码错误!");
        }
    }

    /**
     * 功能描述: 用户注册
     * @Param: [user]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/14 16:27
     */
    public Result saveUser(User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //判断该账号是否已存在
        wrapper.eq("account",user.getAccount());
        //账号已存在就注册失败，否则注册成功
        if (getOne(wrapper) == null){
            save(user);
            User user1 = getById(user.getId());
            //注册成功返回token和用户信息
            return new Result<>().GetToken(user1,tokenUtil.getToken(user1.getId().toString(),"user"));
        }
        else {
            return new Result<>().OK("该用户已存在");
        }
    }

    /**
     * 功能描述: 获取所有用户信息
     * @Param: []
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/14 18:54
     */
    public Result saveAll(){
        List<User> list = list();
        return new Result<>().Huoqu_OK(list);
    }

    /**
     * 功能描述: 根据id更新用户
     * @Param: [user]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/14 19:07
     */
    public Result updateUser(User user){
        updateById(user);
        return new Result<>().OK("更新成功");
    }

    /**
     * 功能描述: 根据id删除用户
     * @Param: [id]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/14 19:10
     */
    public Result deleteUser(Integer id){
        removeById(id);
        return new Result<>().OK("删除成功");
    }

    /**
     * 功能描述: 根据id批量删除用户
     * @Param: [ids]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/14 19:52
     */
    public Result deleteByIdList(Map<String,List> ids){
        //获取id集合
        List<Integer> list = ids.get("ids");
        //遍历集合根据id删除用户
        list.forEach(e -> removeById(e));
        return new Result<>().OK("批量删除成功!");
    }

    /**
     * 功能描述: 根据id批量编辑用户
     * @Param: [users]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/14 23:51
     */
    public Result updateByUserList(Map<String,List<User>> users){
        //获取用户集合
        List<User> list = users.get("users");
        //遍历集合依次根据id更新用户信息
        for (User u : list) {
            updateById(u);
        }
        return new Result<>().OK("批量编辑成功！");
    }

    /**
     * 功能描述: 模糊查询用户
     * @Param: [name]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/15 0:45
     */
    public Result likeByName(String name){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //查找用户姓名或昵称中和 传来的值 有关的
        queryWrapper.like("name",name).or().like("nikName",name);
        List<User> list = list(queryWrapper);
        if (!list.isEmpty()){
            return new Result<>().Huoqu_OK(list);
        }
        return new Result<>().OK("查询失败!");
    }

    /**
     * 功能描述: 用户分页查询
     * @Param: [page]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/15 20:34
     */
    public Result findPage(@PathVariable Integer p){
        //参数：当前页
        Page<User> page1 = new Page(p-1,10);
        page(page1);
        List<User> users = page1.getRecords();
        return new Result<>().Huoqu_OK(users);
    }
}
